package com.anolesoft.amljmes.delivery.deliwebservice.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.anolesoft.amljmes.delivery.deliwebservice.dto.ChuRuKuDto;
import com.anolesoft.amljmes.delivery.deliwebservice.dto.MesBzzScjljhDto;
import com.anolesoft.amljmes.delivery.deliwebservice.service.DeliWebService;
import com.anolesoft.common.core.domain.LoginUser;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class DeliWebServiceImpl implements DeliWebService {

//    private final String QJWLURL = "http://192.168.8.106:8080/services/erpWebService?wsdl";
    private final String QJWLURL = "http://localhost:8081/services/erpWebService?wsdl";
//     private final String QJWLURL = "http://172.17.62.246:8082/services/erpWebService?wsdl";

    /**
     * 全景物流获取计量计划页面数据
     * @param mesBzzScjljhDto
     * @return
     * @throws Exception
     */
    @Override
    public List<MesBzzScjljhDto> getArrList(MesBzzScjljhDto mesBzzScjljhDto) throws Exception {
        LoginUser loginUser = com.anolesoft.yr.util.SecurityUtils.getLoginUser();
        String fcode = loginUser.getFacCode();
//        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
//        Client client = dcf.createClient(QJWLURL);
        Client client = SpeedImpl.getInstance();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("fcode",fcode);
        jsonObject.put("model",mesBzzScjljhDto.getModel());
        jsonObject.put("tuhao",mesBzzScjljhDto.getPartDrawingNo());
        jsonObject.put("pgh",mesBzzScjljhDto.getPgh());
        if (mesBzzScjljhDto.getQualifiedLabelNo() != null) {
            jsonObject.put("hgbq",mesBzzScjljhDto.getQualifiedLabelNo());
            jsonObject.put("fhq",mesBzzScjljhDto.getFhq());
            jsonObject.put("jlq",mesBzzScjljhDto.getJlq());
        }
        Object[] objects = new Object[0];
        objects = client.invoke("getjljhList", jsonObject.toString());
        Object arrStr = objects[0];
        String jsonObjectStr = arrStr.toString();
        JSONArray jsonArray = JSONArray.parseArray(jsonObjectStr);
        List<MesBzzScjljhDto> list = JSONObject.parseArray(jsonArray.toJSONString(), MesBzzScjljhDto.class);
//        List<MesBzzScjljhDto> list = new ArrayList<>();
//        MesBzzScjljhDto aaaa = new MesBzzScjljhDto();
//        MesBzzScjljhDto bbbb = new MesBzzScjljhDto();
//        MesBzzScjljhDto cccc = new MesBzzScjljhDto();
//        aaaa.setJfcj("1");
//        aaaa.setJscj("46");
//        aaaa.setKyNum("3");
//        aaaa.setObjuid("111111");
//        bbbb.setJfcj("13");
//        bbbb.setJscj("46");
//        bbbb.setKyNum("3");
//        bbbb.setObjuid("222222");
//        bbbb.setJhZt("1");
//        cccc.setJfcj("13");
//        cccc.setJscj("46");
//        cccc.setKyNum("3");
//        cccc.setObjuid("33333");
//        cccc.setJhZt("1");
//        list.add(aaaa);
//        list.add(bbbb);
//        list.add(cccc);
        return list;
    }

    /**
     * 点击生成计量计划反馈全境物流,修改库存子表计划类型
     * @param objuids
     * @return
     * @throws Exception
     */
    @Override
    public int updateIns(List<String> objuids) throws Exception {
        // 将objuids转换成()的字符串
        String old = objuids.toString();
        String oldStr = old.replace("[","");
        String newStr = oldStr.replace("]","");
        String newStr1 = newStr.replace(" ","");
//        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
//        Client client = dcf.createClient(QJWLURL);
        Client client = SpeedImpl.getInstance();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("objuids",newStr1);
        Object[] objects = new Object[0];
        objects = client.invoke("updateJhzt", jsonObject.toString());
        Object arrStr = objects[0];
        int a = Integer.parseInt(arrStr.toString());
//        int a = 1;
        return a;
    }

    /**
     * 三期计划库房出库后,库存不可用;入库后库存可用
     * @param dto
     * @return
     */
    @Override
    public String chuku(ChuRuKuDto dto) {
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient(QJWLURL);
//        Client client = SpeedImpl.getInstance();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("flag",dto.getFlag());
        jsonObject.put("hgbq",dto.getHgbq());
        Object[] objects = new Object[0];
        Object arrStr = null;
        try {
            objects = client.invoke("churuku", jsonObject.toString());
            arrStr = objects[0];
        } catch (Exception e) {
            e.printStackTrace();
        }
        return arrStr.toString();
    }
}
